using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.System.Dtos.Province;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 行政区划
    /// 版 本：V2.6.200509
    /// 版 权：引迈信息技术有限公司（https://www.yinmaisoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-11-13
    /// </summary>
    [Route("api/System/[controller]")]
    public class ProvinceController : BaseApiController
    {
        private ProvinceBll provinceBll = new ProvinceBll();

        #region GET
        /// <summary>
        /// 获取行政区划列表
        /// </summary>
        /// <param name="nodeid">节点Id</param>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet("{nodeId}")]
        public async Task<ResponseResult> GetList([FromQuery] KeywordInput input, string nodeid)
        {
            var data = await provinceBll.GetList(nodeid);
            if (!string.IsNullOrEmpty(input.keyword))
            {
                data = data.FindAll(t => t.EnCode.Contains(input.keyword) || t.FullName.Contains(input.keyword));
            }
            var output = data.MapToList<ProvinceListOutput>();
            foreach (var item in output)
            {
                item.isLeaf = (await provinceBll.GetList(item.id)).Count > 0 ? false : true;
                item.hasChildren = (await provinceBll.GetList(item.id)).Count > 0 ? true : false;
            }
            return Success(new { list = output });
        }

        /// <summary>
        /// 获取行政区划下拉框数据(异步)
        /// </summary>
        /// <param name="id">当前Id</param>
        /// <returns></returns>
        [HttpGet("{id}/Selector")]
        public async Task<ResponseResult> GetSelector(string id)
        {
            var data = await provinceBll.GetList(id);
            var output = data.MapToList<ProvinceSelectorOutput>();
            foreach (var item in output)
            {
                item.isLeaf = (await provinceBll.GetList(item.id)).Count > 0 ? false : true;
            }
            return Success(new { list = output.OrderByDescending(x => x.sortCode).ToList() });
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpGet("{id}/Info")]
        public async Task<ResponseResult> GetInfo(string id)
        {
            var data = (await provinceBll.GetInfo(id)).MapTo<ProvinceInfoOutput>();
            return Success(data);
        }
        #endregion

        #region POST
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(string id)
        {
            if ((await provinceBll.GetList(id)).Count == 0)
            {
                var entity = await provinceBll.GetInfo(id);
                if (entity != null)
                {
                    await provinceBll.Delete(entity);
                }
                return Success("删除成功");
            }
            else
            {
                return Error("删除失败，当前有子节点数据");
            }
        }

        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Create([FromBody] ProvinceCrInput input)
        {
            if (await provinceBll.IsExistByEnCode(input.enCode, null))
            {
                return Error("编码已存在");
            }
            var entity = input.MapTo<ProvinceEntity>();
            await provinceBll.Create(entity);
            return Success("新建成功");
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(string id, [FromBody] ProvinceUpInput input)
        {
            if (await provinceBll.IsExistByEnCode(input.enCode, id))
            {
                return Error("编码已存在");
            }
            var entity = input.MapTo<ProvinceEntity>();
            await provinceBll.Update(id, entity);
            return Success("更新成功");
        }

        /// <summary>
        /// 更新行政区划状态
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpPut("{id}/Actions/State")]
        public async Task<ResponseResult> ActionsState(string id)
        {
            var entity = await provinceBll.GetInfo(id);
            entity.EnabledMark = entity.EnabledMark == 0 ? 1 : 0;
            var flag = await provinceBll.Update(id, entity);
            return flag > 0 ? Success("更新成功") : Error("更新失败");
        }
        #endregion
    }
}
